//jQuery plugins
(function($) {
	$.fn.serializeObject = function() {
		var o = {};
		var a = this.serializeArray();
		$.each(a, function() {
			var key = this.name;
			var value = this.value || '';
			var obj = null;
			if (key.indexOf('.') > 0) {
				var keys = key.split('.');
				obj = o[keys[0]];
				if (!obj) {
					obj = {};
					o[keys[0]] = obj;
				}
				key = keys[1];
			} else {
				obj = o;
			}
			if (typeof obj[key] != 'undefined') {
				if (!obj[key].push) {
					obj[key] = [ obj[key] ];
				}
				obj[key].push(value);
			} else {
				obj[key] = value;
			}
		});
		return o;
	};

	$.preloader = (function() {
		var _loadingCount = 0;

		var show = function() {
			_loadingCount++;
			var mask = document.getElementById('loading-indicator-mask');
			if (mask) {
				$(mask).css({
					'display' : 'block'
				});
			} else {
				try {
					$('<div id="loading-indicator-mask"/>').appendTo(
							document.body).append(
							'<div id="loading-indicator"/>');
				} catch (e) {
				}
			}
		};

		var hide = function() {
			_loadingCount--;
			if (_loadingCount > 0) {
				return;
			}
			var mask = document.getElementById('loading-indicator-mask');
			if (mask) {
				$(mask).css({
					'display' : 'none'
				});
			}
		};

		return {
			show : show,
			hide : hide
		};
	})();
	
	$.fn.onPositionChanged = function (trigger, millis) {
	    if (millis == null) millis = 100;
	    var o = $(this[0]); // our jquery object
	    if (o.length < 1) return o;

	    var lastPos = null;
	    var lastOff = null;
	    setInterval(function () {
	        if (o == null || o.length < 1) return o; // abort if element is non existend eny more
	        var oid = $('#'+o.attr('id'));
	        if(oid.parents('.selectfile').hasClass('selectfiledialog')){
	        	o = oid;
	        }
	       
	        if (lastPos == null) lastPos = o.position();
	        if (lastOff == null) lastOff = o.offset();
	        var newPos = o.position();
	        var newOff = o.offset();
	        if (lastPos.top != newPos.top || lastPos.left != newPos.left) {
	            $(this).trigger('onPositionChanged', { lastPos: lastPos, newPos: newPos });
	            if (typeof (trigger) == "function") trigger(lastPos, newPos);
	            lastPos = o.position();
	        }
	        if (lastOff.top != newOff.top || lastOff.left != newOff.left) {
	            $(this).trigger('onOffsetChanged', { lastOff: lastOff, newOff: newOff});
	            if (typeof (trigger) == "function") trigger(lastOff, newOff);
	            lastOff= o.offset();
	        }
	    }, millis);

	    return o;
	};

})(jQuery);

if (!JSON || !JSON.stringify) {
	if (!JSON) {
		var JSON = {};
	}
	JSON.stringify = function(obj) {
		var t = typeof (obj);
		if (t != "object" || obj === null) {
			// simple data type
			if (t == "string")
				obj = '"' + obj + '"';
			return String(obj);
		} else {
			// recurse array or object
			var n, v, json = [], arr = (obj && obj.constructor == Array);
			for (n in obj) {
				v = obj[n];
				t = typeof (v);
				if (t == "string")
					v = '"' + v + '"';
				else if (t == "object" && v !== null)
					v = JSON.stringify(v);
				json.push((arr ? "" : '"' + n + '":') + String(v));
			}
			return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
		}
	};
	JSON.parse = function(s) {
		if (!s) {
			return null;
		}
		return window.eval('(' + s + ')');
	};
}


var syncSelectLabel=function(id,lid){
	var o="";
	var opt = $("#"+id).find('option');
    opt.each(function(i) {
        if (opt[i].selected == true) {
            o = opt[i].innerHTML;
        }
    });
    $("#"+lid).html(o);
}